package com.grandbroad.eim.ui.fragment.vidoe;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.easy.west.westclient.api.base.bean.ResultBean;
import com.easy.west.westclient.api.base.bean.VodBean;
import com.easy.west.westclient.api.hotvod.bean.VodList;
import com.easy.west.westclient.api.hotvod.event.GetHotVodListEvent;
import com.easy.west.westclient.api.login.bean.User;
import com.easy.west.westclient.api.user.event.FollowEvent;
import com.easy.west.westclient.api.user.event.IsFollowEvent;
import com.easy.west.westclient.api.vod.event.CancleCollectVodEvent;
import com.easy.west.westclient.api.vod.event.CollectEvent;
import com.easy.west.westclient.api.vod.event.IsCollectVodEvent;
import com.grandbroad.eim.adapter.VideoDesAdapter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseFragment;
import com.grandbroad.eim.base.BaseRecyclerview;
import com.grandbroad.eim.utils.CacheUtils;
import com.grandbroad.eim.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 点播简介页面
 */
public class DescribleFragment extends BaseRecyclerview {
    private Bundle bundle;
    private VodBean vodBean;
    private VideoDesAdapter desAdapter;
    //返回值很傻叉不能用lib请求，未加入单独的字符串结果
    private String follow_url = "https://api.gaobohuike.com/demo-0.0.1-SNAPSHOT/follows/create";
    private String cancel_url = "https://api.gaobohuike.com/demo-0.0.1-SNAPSHOT/follows/delete";
    @Override
    protected boolean setEventOpen() {
        return true;
    }

    @Override
    protected void initRecyclerview(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(desAdapter);
        baseRecyclerview.setRefreshing(true);
        baseRecyclerview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getRecommendVod();
            }
        });
        initsession();
    }

    /**
     * 点击事件
     */
    private void initsession() {
        //关注/取消关注
        desAdapter.setFollowBtnClickListener(new VideoDesAdapter.onFollowBtnClickListener() {
            @Override
            public void click(int pos, Object o) {
                VodBean  thisbean = (VodBean) o;
                if (desAdapter.isFollow())
                {
                    //取消关注
                    showNormalDialog(false, "提示", "确定要取消关注吗?", "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            unfollow(thisbean);
                        }
                    });

                }
                else
                {
                    //关注
                    follow(thisbean);
                    showProgessDialog("加载中");
                }



            }
        });
        //收藏/取消收藏
        desAdapter.setOncollectionClickListener(new VideoDesAdapter.oncollectionClickListener() {
            @Override
            public void click(int pos, Object o) {
                VodBean vodBean = (VodBean) o;
                if (desAdapter.isCollect())
                {
                    //取消收藏
                    showNormalDialog(false, "提示", "确定要取消收藏吗?", "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            uncollect(vodBean);
                        }
                    });
                }
                else
                {
                    //收藏
                    collection(vodBean);
                }

            }
        });
    }

    /**
     * 收藏处理
     * @param vodBean
     */
    private void uncollect(VodBean vodBean) {
        MyApplication.getMoleclient().cancelVod(CacheUtils.getUser().getId(),vodBean.getId());
        showProgessDialog("请求中");

    }

    private void collection(VodBean vodBean) {
        MyApplication.getMoleclient().colloctVod(CacheUtils.getUser().getId(),vodBean.getId());
        showProgessDialog("请求中");
    }

    /**
     * 取消关注
     * @param thisbean
     */
    private void unfollow(VodBean thisbean) {
       RequestParams params = new RequestParams(cancel_url);
       params.addBodyParameter("access_token",CacheUtils.getUser().getAccess_token());
       params.addBodyParameter("targetId", String.valueOf(thisbean.getUser().getId()));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result)
            {
                if (result.equals("success"))
                {
                    ToastUtils.show("取消关注成功");
                    desAdapter.setFollow(false);
                    dissmissDialog();
                }
                else
                {
                    ToastUtils.show("加载失败:"+result);
                    dissmissDialog();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 关注
     * @param thisbean
     */
    private void follow(VodBean thisbean) {
        RequestParams params = new RequestParams(follow_url);
        params.addBodyParameter("access_token",CacheUtils.getUser().getAccess_token());
        params.addBodyParameter("ownerId", String.valueOf(CacheUtils.getUser().getId()));
        params.addBodyParameter("followedId", String.valueOf(vodBean.getUser().getId()));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success"))
                {
                    ToastUtils.show("关注成功");
                    desAdapter.setFollow(true);
                    dissmissDialog();
                }
                else
                {
                    ToastUtils.show("加载失败:"+result);
                    dissmissDialog();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Override
    protected void loadData()
    {
        User user = CacheUtils.getUser();
        bundle = getArguments();
        vodBean = (VodBean) bundle.getSerializable("bean");
        //getNet
        getRecommendVod();

        desAdapter = new VideoDesAdapter((AppCompatActivity) getActivity());
        desAdapter.setThisbean(vodBean);
        desAdapter.notifyDataSetChanged();

    }

    private void getRecommendVod() {
        //是否收藏了
        MyApplication.getMoleclient().isColloect(CacheUtils.getUser().getId(),vodBean.getId());
        //是否关注了
        MyApplication.getMoleclient().isfollow(CacheUtils.getUser().getAccess_token(),vodBean.getUser().getId());
        //获取热门点播
        MyApplication.getMoleclient().getHotVod();
    }

    /**
     * 收藏
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void collect(CollectEvent eva)
    {
        if (eva.isOk())
        {
            ResultBean resultBean = eva.getBean();
            if (resultBean.getStatus()==1)
            {
                //收藏成功
                desAdapter.setCollect(true);
            }
            else
            {
                ToastUtils.show("关注失败:"+resultBean.getErrorDesp());
            }

        }
        dissmissDialog();
    }

    /**
     * 判断是否收藏了
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void iscollect(IsCollectVodEvent eva)
    {
        if (eva.isOk())
        {
            boolean iscollect = eva.getBean();
            desAdapter.setCollect(iscollect);
        }
    }
    /**
     * 取消收藏
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void cancelCollect(CancleCollectVodEvent eva)
    {
        if (eva.isOk())
        {
            ResultBean resultBean = eva.getBean();
            if (resultBean.getStatus()==1)
            {
                //取消收藏成功
                desAdapter.setCollect(false);
            }
            else
            {
                ToastUtils.show("关注失败:"+resultBean.getErrorDesp());
            }
        }
        dissmissDialog();
    }
    /**
     * 是否关注了该用户
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void isfollow(IsFollowEvent eva)
    {
        if (eva.isOk())
        {
            boolean is = eva.getBean();
            if (is)
            {
                desAdapter.setFollow(true);
            }
            else
            {
                desAdapter.setFollow(false);
            }
        }
    }
    /**
     * 获取热门列表
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHotVod(GetHotVodListEvent eva)
    {
        if (eva.isOk())
        {
            VodList list = eva.getBean();
            if (list.getStatus()==1)
            {
                stopRefresh();
                desAdapter.setVodBeanList(list.getData());
                desAdapter.notifyDataSetChanged();
            }
        }
        else {
            stopRefresh();
        }
    }
}
